<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 贝贝 <hebiduhebi@163.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------

namespace Admin\Controller;

use Shop\Api\ProductGroupApi;

class ProductGroupController extends AdminController{
		
	public function add(){
		$product_id = I('post.product_id',0);
		$groups = I('post.groups',array());
		if($product_id == 0){
			$this->error("参数错误！");
		}
        //1. 删除之前的分组
        $map['p_id'] = $product_id;
        $result = apiCall(ProductGroupApi::DELETE , array($map));

		foreach($groups as $key=>$group_id){
            $entity  = array(
                'p_id'=>$product_id,
                'g_id'=>$group_id,
            );
            $entity['start_time']=strtotime(I('post.start_time_'.$group_id,''));
            $entity['end_time']=strtotime(I('post.end_time_'.$group_id,''));
            $entity['price']= I('post.price_'.$group_id,0,'floatval');

            //2. 新增分组
			$result = apiCall(ProductGroupApi::ADD, array($entity));
			if(!$result['status']){
				$this->error($result['info']);
			}
		}
		
		if($result['status']){
			$this->success("操作成功！");
		}else{
			$this->error($result['info']);
		}
	}
	
}
